/* 详情页面 SKU
 * wangjingliang 2016-04-16
 */
;(function($, ECar) {
    var dimSku = [];
    //当前选中的维度对象
    var goodType=$('.good_choose .good_type'),//具有几个维度
        typeItem=goodType.find('.selected'),
        dimNum=goodType.length;
    var arrDefault=['CX4','CX1','AY_2D0101_attr_14'];//默认选中的维度
    
    //创建数据集合
    goodType.each(function(i){      
        $(this).find('div[data-value='+arrDefault[i]+']').addClass('selected');
        var $this = $(this),
            selected = $this.find('.selected'),
            selectedValue = arrDefault[i],
            unselectObj = selected.siblings().not('.disable');
            dimSku[i]={                
                className:'selected',
                selectedObj:selected,
                unselectObj:unselectObj,
                vdLine:i,                
                selectedValue:selectedValue,
                _selectedValue:selectedValue
            };
    });

    function setStyle(obj){ //点击设置样式
        obj.addClass('selected').siblings().removeClass('selected');
    }

    function isCanClick(obj){  //是否可以点击
        return obj.hasClass('selected')||obj.hasClass('disable');
    }

    function getCurrentValue(obj){ //当前值
        return obj.data('value');
    }

    function setValue(value,i,obj,unselectObj){ //获取维度值
        dimSku[i] = {                
            className:'selected',
            selectedObj:obj,
            unselectObj:unselectObj,
            vdLine:i,           
            selectedValue:value,
            _selectedValue:value
        };
    }

    function getSkuString(name) {//通过sku查找skucode
        var str = [];
        if(dimNum > 1){
            for(var i in dimSku){           
                str.push(dimSku[i].selectedValue);          
            }
            return str.join('_');   
        }else{
            return dimSku[0].selectedValue;
        }             
    }

    //获取具体商品
    function getSkuCode(str){
        return attrMap[str];
    }

    function getSkuStatus(str){
        return skuStatusMap[str];
    }

    function setStyleAfter(n, obj) {
        if(n == 2 || n == undefined) {
            obj.addClass('disable');
        } else {
            obj.removeClass('disable');
        }
    }

    //当前未被选中的商品组合 遍列上下架情况
    function getUnProductColl(dim, dimSku) {        
        var arr = [];
        for(var i=0;i<dim;i++) {
            dimSku[i].unselectObj.each(function(j) {                
                var $this = $(this),
                    arrPro = [];
                dimSku[i]._selectedValue = $this.data('value');              
                for(var k=0; k<dim; k++){                    
                    arrPro.push(dimSku[k].selectedValue);
                }                
                arrPro[i] = dimSku[i]._selectedValue;
                var skucode = getSkuCode(arrToSting(arrPro));
                var num = getSkuStatus(skucode);
                setStyleAfter(num, $this);
                arr.push(arrPro);
            });
        }
        return arr;
    }

    function arrToSting(strArr){
        return strArr.join('_');
    }

    function itemClickAfter(obj,currentDim){        
        setStyle(obj);
        setValue(getCurrentValue(obj), currentDim,obj,obj.siblings().not('.disable'));
        var skutring=getSkuString('selectedValue');
            getSkuCode(skutring);
            getUnProductColl(dimNum,dimSku);
    }

    //获取库存情况
    function getInventory(backfn) {     
         $.ajax({
                url: '/mdseDetail_v2/getSkuInfo.htm',
                type:  'post',
                data:{'skuCode':105641},
                callback:backfn||$.noop,
                success:function(data){ 
                    var data=$.parseJSON(data);
                    backfn(data);
                }
            });
    }
    
    function init(dimNum,dimSku){
        getUnProductColl(dimNum,dimSku)
        
    }
    
    function ajaxBackSetText(data){
        var cashPrice = data.cashPrice,    //实际价格
            marketPrice = data.marketPrice,//市场价格
            pointPrice = data.pointPrice,  //积分
            promotion = data.promotion  //促销信息
           
    }

    $('.type_item').on('click',function(){  //setstyle      
        var $this=$(this);        
        if(isCanClick($this))return;
        var currentDim=$this.parents('.good_type').index();//当前维度
        itemClickAfter($this, currentDim);
        getInventory(ajaxBack);
    });
    
    //初始化页面        
    init(dimNum,dimSku);
    


    //console.info(dimSku);

})(jQuery, window.ECar || (window.ECar = {}));
